package com.anzia.storeback.dao;

import com.anzia.storeback.entity.Category;
import com.anzia.storeback.entity.Product;
import com.anzia.storeback.entity.Product_picture;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *@ClassName ProductDao
 *@Description
 *@Author Anzia.
 *@Date 2021/5/29 20:56
 *@Version 1.0
 **/
@Repository
public interface ProductDao {

    // 获得类别信息
    @Select("select * from category")
    List<Category> getCategory();

    // 获得所有商品信息
    @Select("select * from product where status=1")
    List<Product> getAllProduct();

    // 根据商品id获得图片
    @Select("select * from product where product_id=#{product_id}")
    List<Product_picture> getDetailsPicture(int product_id);

    // 根据商品id获得商品信息
    @Select("select p.* from product p, secgoods sec where sec.product_id=p.product_id and sec.id=#{secgood_id}")
    Product getProductByID(int product_id);

    // 获得类别的所有id
    @Select("select category_id from category")
    List<Integer> getCategory_id();

    // 根据分类id获得分页后的商品信息
    @Select("select * from product where category_id={category_ID} and status=1 order by product_sales desc limit #{currentPage}, #{pageSize}")
    List<Product> getProductByCategory(int categoryID, int currentPage, int pageSize);

    // 根据分类名称获得商品信息
    @Select("select p.* from product p, category c where p.category_id=c.category_id and c.category_name=#{categoryName} and p.status=1")
    List<Product> getProductByCategoryName(String categoryName);

    // 根据商品名称模糊查询商品信息
    @Select("select * from product where status=1 and product_name like '%${search}%' or product_title like '%${search}%' or product_intro like '%${search}%'")
    List<Product> getProductBySearch(String search);

    // 根据商品id查询销量
    @Select("select product_sales from product where product_id={product_id}")
    Integer getSales(int product_id);

    // 根据商品id更新商品销量
    @Update("update product set product_sales=product_sales+#{sales} where product_id=#{product_id}")
    void updateSales(int product_id, int sales);

}
